package com.epro.dx.task;

import java.util.List;

import com.epro.dx.listenner.MyAsyncTaskListenner;
import com.epro.dx.util.ActivityUtil;
import com.epro.dx.util.OnekeyFilterLocalUtil;
import com.google.android.gms.internal.my;
import com.weipu.common.facade.exception.DxException;
import com.weipu.common.facade.factory.ProviderFactory;
import com.weipu.common.facade.model.CatelogModel;
import com.weipu.common.facade.model.OneKeyFilterResponseModel;

import android.content.Context;
import android.os.AsyncTask;

/************************
 * @author Tony
 * @Date:2016-3-29下午3:33:10
 * @描述:获取用户一键过滤的设置信息的任务
 ************************/
public class SaveOneKeyFilterSettingsAsyncTask extends BaseNeedReLoginTask {

	private MyAsyncTaskListenner myAsyncTaskListenner;
	private OneKeyFilterResponseModel customerFilter;
	private boolean isFilter;
	private String blackCategoryIds;
	private Context mContext;
	private List<CatelogModel> blackCategorys;
	private List<CatelogModel> whiteCategorys;
	public SaveOneKeyFilterSettingsAsyncTask(Context mContext,boolean isFilter,String blackCategoryIds,
			List<CatelogModel> blackCategorys,List<CatelogModel> whiteCategorys,MyAsyncTaskListenner myAsyncTaskListenner){
		this.mContext = mContext;
		this.isFilter = isFilter;
		this.blackCategoryIds = blackCategoryIds;
		this.blackCategorys = blackCategorys;
		this.whiteCategorys = whiteCategorys;
		this.myAsyncTaskListenner = myAsyncTaskListenner;
	}
	@Override
	protected void onPreExecute() {
		if(null!=myAsyncTaskListenner){
			myAsyncTaskListenner.onPreTask();
		}
	}
	@Override
	protected String doInBackground(Void... params) {
		try {
			if(ActivityUtil.isLogin(mContext)){
				//登录了，调用接口保存
				customerFilter = ProviderFactory.createUserServiceProvider().saveCustomerFilter(ActivityUtil.getCustomerToken(),
						ActivityUtil.getCustomerID(), isFilter, blackCategoryIds);
			}
			//本地保存
			long cid = ActivityUtil.isLogin(mContext)?ActivityUtil.getCustomerID():-1;
			OnekeyFilterLocalUtil.deleteLocalData(mContext, cid);
			OnekeyFilterLocalUtil.saveToLocal(mContext, cid, blackCategorys, false);
			OnekeyFilterLocalUtil.saveToLocal(mContext, cid, whiteCategorys, true);
		} catch (DxException e) {
			return e.getErrCode();
		}
		return "0";
	}
	@Override
	protected void onPostExecute(String result) {
		super.onPostExecute(result);
		if(null!=myAsyncTaskListenner && !isRelogin){
			myAsyncTaskListenner.onPostTask(result, customerFilter);
		}
	}
	@Override
	protected Context setContext() {
		return mContext;
	}
	@Override
	protected BaseNeedReLoginTask setTask() {
		return new SaveOneKeyFilterSettingsAsyncTask(mContext, isFilter, blackCategoryIds, blackCategorys, whiteCategorys, myAsyncTaskListenner);
	}

}
